var EVENT_AV = 0; // 加速度事件类型
var EVENT_VIDEO = 1; // 开启摄像头事件
var last_send_time = 0;

var timing_seconds = -1; // 播放声音计时任务记录秒数
var timer_id; // 相当于句柄一样的东西
var audio_seconds = 10; // 播放声音秒数的参数，默认为10秒

function DeviceEvent(deviceHash, eventType) {
	this.deviceHash = deviceHash;
	this.eventType = eventType;
}

function messagehandler(message) {
	
	var RTCmessage = JSON.parse(message);
	
	console.log(RTCmessage);
	
	if(RTCmessage.sdp){
		
		
		
		if(!con){
			con =new peerConnection(null);
			
		//	console.log("src is null");
		//	reVideo.src = null;
			
			con.onIceCandidate = function (event){
	  			if(event.candidate){
	  				//发送消息candidate:event.candidate候选人信息
	  				console.log("candidate");
	  			}
	  		};
	  		
	  		con.onaddstream = function (event) {
	  			attachMediaStream(reVideo, event.stream);
	  		};
		}
		
  		console.log("set remoteDescription");
  		
		 var offer = new sessionDescription(RTCmessage.sdp);
		 
		 console.log(offer);
		 
		 con.setRemoteDescription(offer,function(){
			 console.log("message in");
	  			if(con.remoteDescription.type = "offer"){
	  				console.log("message offer");
	  				con.createAnswer(function(desc){
	  					con.setLocalDescription(desc, function(){
	  		  				//发送消息 con.localDescription
	  		  				console.log("send localDescription");
	  		  				websocket.send(JSON.stringify({
	  							       'sdp': con.localDescription
	  							     }));
	  		  			});
	  				}, function(failureCallback){
	  					console.log(failureCallback);
	  				});
	  				
	  			}
	  		},function(errorInformation){
	  			console.log(errorInformation);
	  		});
	 }
	
	else if(RTCmessage.cal){
		 createPeerConnection();
	} else {
		
		var json = eval("(" + message + ")");
		
		console.log(json);

		switch (json.eventType) {
		case EVENT_AV:
			// alert("事件"+EVENT_AV);
			$.post("getDeviceInfo.action", {
				"device.deviceHash" : guid
			}, function(response) {
				switch (response.result) {
				case "success":
					var curEquipment = response.device;
					if (curEquipment.isOnline) {
						if (curEquipment.isKey) {
							self.location = "#alarm";
						} else {
							playAlarm();
							self.location = "#monitor";
						}

					}
					break;
				case "error":
					alert(response.reason);
					break;
				}
			}, "json");
			
			break;
		case EVENT_VIDEO:
			// alert("事件"+EVENT_VIDEO);
			break;
		}
		
	}

	



}

function playAlarm() {
	// 不支持IE8以下
	$("body").append(
			"<audio id='alarm' src='sound/alarm.wav' loop autoplay>"
					+ "<source src='sound/alarm.mp3' type='audio/mpeg' />"
					+ "<source src='sound/alarm.wav' type='audio/wav' />"
					+ "</audio>");
	startTiming(5);
}

function stopAlarm() {
	$("#alarm").remove();
}

// 开始计时播放
function startTiming(second) {
	if (second) {
		audio_seconds = second;
	}
	timing_seconds += 1;
	// $('#testss').text(timing_seconds);
	if (timing_seconds == audio_seconds) {
		stopAlarm(); // 停止播放
		stopTiming(); // 停止计时
	} else {
		timer_id = setTimeout("startTiming()", 1000);
	}
}

// 停止计时播放，还原一些参数
function stopTiming() {
	audio_seconds = 10;
	timing_seconds = -1;
	clearTimeout(timer_id);
}

function eventsender(event_type) {
	var cur_time = new Date().getTime();
	if (cur_time - last_send_time > 10000) { // 保证十秒内不重复发送数据
		var deviceHash = getGuid();
		var deviceEvent = new DeviceEvent(deviceHash, event_type);
		websocket_send(JSON.stringify(deviceEvent));
		last_send_time = cur_time;
	}
}